package com.kgo.jwt.controller;

import com.kgo.jwt.entity.LoginEntity;
import com.kgo.jwt.service.LoginService;
import com.kgo.jwt.sysparams.JWTParams;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author keepgoon
 * @Description: TODO
 * @date 2019/3/19:40
 */
@RestController
@CrossOrigin
public class LoginController {
    @Resource
    private LoginService loginService;

    @RequestMapping(value = "/login", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
    public String login(HttpServletRequest request , HttpServletResponse response, LoginEntity login){
        String tokenStr =  loginService.login(login);

        Cookie cookie = new Cookie(JWTParams.Token,tokenStr);
        response.addCookie(cookie);
        return tokenStr;
    }
}
